﻿/*****************************************************************
 *Yuga Technology
 *@author Oplink OMS
 *@copyright all rights reserved (c) zengkang007@gmail.com
 *****************************************************************/
package com.oplink.webems.modules.sys.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;

import com.oplink.webems.common.persistence.Page;
import com.oplink.webems.modules.sys.entity.RoleMenu;
import com.oplink.webems.modules.sys.entity.User;
import com.oplink.webems.modules.sys.service.RoleMenuService;
import com.oplink.webems.modules.sys.service.ShiroDbRealm.ShiroUser;

/**
 * RoleMenu管理的Controller, 使用Restful风格的Urls:
 * 
 * List page : GET /queue/
 * Create pushlet : GET /queue/pushlet
 * Create page : GET /queue/create
 * Create action : POST /queue/create
 * Update page : GET /queue/update/{id}
 * Update action : POST /queue/update
 * Delete action : GET /queue/delete/{id}
 * 告警Controller
 * 
 * @author Oplink OMS
 * @version 2014-09-02
 */
@Controller
@RequestMapping(value = "sys/roleMenu")
public class RoleMenuController extends BaseController {

	@Autowired
	private RoleMenuService roleMenuService;

	@RequiresPermissions("modules/sys:roleMenu:view")
	@RequestMapping(value = { "list", "" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String list(RoleMenu roleMenu, HttpServletRequest request, HttpServletResponse response, Model model) {

		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
		Long userId = getCurrentUserId();

		Page<RoleMenu> pages = roleMenuService.find(new Page<RoleMenu>(request, response), roleMenu);
		// Page<RoleMenu> pages = roleMenuService.getUserRoleMenu(userId, searchParams, pageNumber, pageSize,
		// sortType);
		model.addAttribute("pages", pages);
		model.addAttribute("userId", userId);

		// 将搜索条件编码成字符串，用于排序，分页的URL
		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
		return "modules/sys/roleMenuList";
	}

	@RequiresPermissions("modules/sys:roleMenu:edit")
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	@ResponseBody
	public RoleMenu updateForm(@PathVariable("id") Long id) {
		RoleMenu roleMenu = roleMenuService.getRoleMenu(id);
		roleMenu.setUser(null);
		return roleMenu;
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(@Valid @ModelAttribute("roleMenu") RoleMenu roleMenu, RedirectAttributes redirectAttributes) {
		try {
			User user = new User(getCurrentUserId());
			roleMenu.setUser(user);
			roleMenuService.saveRoleMenu(roleMenu);
			redirectAttributes.addFlashAttribute("message", "更新任务成功");
			return "1";
		} catch (Exception e) {
			return "0";
		}
	}

	@RequiresPermissions("modules/sys:roleMenu:view")
	@RequestMapping(value = "form")
	public String form(RoleMenu roleMenu, Model model) {
		model.addAttribute("roleMenu", roleMenu);
		return "modules/sys/roleMenuForm";
	}

	@RequiresPermissions("modules/sys:roleMenu:edit")
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("userId", getCurrentUserId());
		model.addAttribute("action", "create");
		return "modules/sys/roleMenu/create";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create(RoleMenu roleMenu, RedirectAttributes redirectAttributes, Model model) {
		try {
			User user = new User(getCurrentUserId());
			roleMenu.setUser(user);
			model.addAttribute("roleMenu", roleMenu);
			roleMenuService.saveRoleMenu(roleMenu);
			redirectAttributes.addFlashAttribute("message", "创建任务成功");
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	@RequestMapping(value = "delete/{id}")
	@ResponseBody
	public String delete(@PathVariable("id") String id, RedirectAttributes redirectAttributes) {
		try {
			String ret = "0";
			if (!"".equals(id)) {
				String[] delList = id.split(",");
				for (String idItem : delList) {
					roleMenuService.deleteRoleMenu(Long.valueOf(idItem));
				}
				redirectAttributes.addFlashAttribute("message", "删除任务成功");
				ret = "1";
			} else {
				ret = "0";
			}
			return ret;
		} catch (Exception e) {
			return "0";
		}
	}

	/**
	 * 取出Shiro中的当前用户Id.
	 */
	private Long getCurrentUserId() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user.id;
	}
}
